<?php

  //////////////////////////////////////////////////////////////////
  // OrbitFAQ                                                     //
  // ---------                                                    //
  //                                                              //
  // Orbit FAQ was solely written and developed by Orbit Services //
  // http://www.orbitservices.net                                 //
  //                                                              //
  // Access the Forum here:                                       //
  // http://forums.orbitservices.net/index.php?c=4                //
  //                                                              //
  // OrbitFAQ utilises the following opensource projects/classes; //
  //  + Fckeditor - http://www.fckeditor.net                      //
  //  + Smarty Template Engine - http://smarty.php.net            //
  //  + Swift Email Class - http://www.swift.com                  //
  //  + OWASP PHP Filter Project - http://www.owasp.org           //
  //  + MySQL Search Class by Stephen Bartholomew                 //
  //                                                              //
  //////////////////////////////////////////////////////////////////

  require('incs/config.inc.php');
  require('incs/db.inc.php');
  require('incs/classes.inc.php');

  if($GatherUserStats == '1')
    {
      require('incs/stats.inc.php');
    }

  $highlight_style = 'background: yellow; color: red;';

  // Get our search Keywords and Sanitize them
  $search_keywords = strip_tags($_POST['search_keywords']);

  $smarty = new Smarty;

  require('incs/common.inc.php');

  $SmartyCompileDir = "skins/$defaultSkin/skins_c";
  $smarty->compile_dir = $SmartyCompileDir;

  $smarty->compile_check = false;
  $smarty->debugging = false;

  // Lets work out where we are
  $wai = "<div>";

    if($defaultFAQ == '')
      {
        $wai_home = $OrbitFAQTitle;
        $wai .= "<a href='index.php'><b>$wai_home</b></a>";

      }

      $wai .= " &raquo; Search Results";

    $wai .= "</div>";

    $smarty->assign("WhereAmI","$wai");
    $smarty->assign("OrbitFAQVersion","$orbitfaq_version");
    $smarty->assign("CategoryShowAnsLevel","$showAlevel");
    $smarty->assign("PageTitle","Search $OrbitFAQTitle");

        $my_qn_search = new MysqlSearch;
        $my_qn_search->setidentifier("id");
        $my_qn_search->settable("orbitfaq_questions");
        $results_qn_array = $my_qn_search->find($search_keywords);

        $my_an_search = new MysqlSearch;
        $my_an_search->setidentifier("id");
        $my_an_search->settable("orbitfaq_answers");
        $results_an_array = $my_an_search->find($search_keywords);

        $ourkw = split(' ', $search_keywords);

        foreach($results_qn_array as $found_qn)
          {

            $query_qn = "SELECT * from orbitfaq_questions WHERE `id` = '$found_qn' AND `status` = '1'";
            $result_qn = $faqsql_query ($query_qn)OR DIE( "$sql_query_error $query_qn");
            $count_qn = $faqsql_count_rows($result_qn);

            while ($row_qn = $faqsql_fetch_array ($result_qn)){
              $qn_id = $row_qn[0];
              $qn_f_id = $row_qn[1];
              $qn_c_id = $row_qn[2];
              $qn_a_id = $row_qn[3];
              $qn_desc = $row_qn[4];

              foreach ($ourkw as $keyword)
                {
                  $qn_desc = highlight($qn_desc,$keyword,$highlight_style);
                  //$qn_desc = str_replace ( $keyword, "<font color=\"FF0000\">$keyword</font>", $qn_desc );
                }

              $added_qns[] = $qn_id;

              $query_ans = "SELECT * from orbitfaq_answers WHERE `id` = '$qn_a_id'";
              $result_ans = $faqsql_query ($query_ans)OR DIE( "$sql_query_error $query_ans");
              $count_ans = $faqsql_count_rows($result_ans);

              while ($row_ans = $faqsql_fetch_array ($result_ans)){
                $ans_id = $row_ans[0];
                $ans_desc = $row_ans[1];
                $ans_posted_by = $row_ans[2];

                $ans_desc = html_entity_decode($ans_desc);

                foreach ($ourkw as $keyword)
                  {
                    $ans_desc = highlight($ans_desc,$keyword,$highlight_style);
                  }

                if($showAlevel == '1')
                  {
                    $ans_desc = ShortenText($ans_desc, $truncateTo);
                  }

              }

                 // Build Smarty Content Array
                 $smarty_qn_array[] = array(
                        "qn_id" => "$qn_id",
                        "qn_f_id" => "$cat_f_id",
                        "qn_c_id" => "$qn_c_id",
                        "qn_desc" => "$qn_desc",
                        "qn_ans_desc" => "$ans_desc"
                  );

            }
          }

        foreach($results_an_array as $found_an)
          {

              $query_ans = "SELECT * from orbitfaq_answers WHERE `id` = '$found_an'";
              $result_ans = $faqsql_query ($query_ans)OR DIE( "$sql_query_error $query_ans");
              $count_ans = $faqsql_count_rows($result_ans);

              while ($row_ans = $faqsql_fetch_array ($result_ans)){
                $ans_id = $row_ans[0];
                $ans_desc = $row_ans[1];
                $ans_posted_by = $row_ans[2];

                $ans_desc = html_entity_decode($ans_desc);

                foreach ($ourkw as $keyword)
                  {
                    $ans_desc = highlight($ans_desc,$keyword,$highlight_style);
                  }

                if($showAlevel == '1')
                  {
                    $ans_desc = ShortenText($ans_desc, $truncateTo);
                  }

                  $query_qn = "SELECT * from orbitfaq_questions WHERE `a_id` = '$ans_id' AND `status` = '1'";
                  $result_qn = $faqsql_query ($query_qn)OR DIE( "$sql_query_error $query_qn");
                  $count_qn = $faqsql_count_rows($result_qn);

                  while ($row_qn = $faqsql_fetch_array ($result_qn)){
                    $qn_id = $row_qn[0];
                    $qn_f_id = $row_qn[1];
                    $qn_c_id = $row_qn[2];
                    $qn_a_id = $row_qn[3];
                    $qn_desc = $row_qn[4];

                    foreach ($ourkw as $keyword)
                      {
                        $qn_desc = highlight($qn_desc,$keyword,$highlight_style);
                      }


                    if($added_qns != '')
                      {
                        if (!in_array($qn_id, $added_qns))
                        {
                           // Build Smarty Content Array
                           $smarty_qn_array[] = array(
                                  "qn_id" => "$qn_id",
                                  "qn_f_id" => "$cat_f_id",
                                  "qn_c_id" => "$qn_c_id",
                                  "qn_desc" => "$qn_desc",
                                  "qn_ans_desc" => "$ans_desc"
                            );
                        }
                      }
                    else
                      {
                         // Build Smarty Content Array
                         $smarty_qn_array[] = array(
                                "qn_id" => "$qn_id",
                                "qn_f_id" => "$cat_f_id",
                                "qn_c_id" => "$qn_c_id",
                                "qn_desc" => "$qn_desc",
                                "qn_ans_desc" => "$ans_desc"
                          );
                      }

                  }

              }

          }

    if(!$smarty_qn_array)
      {
        $smarty->assign('NoContent','1');
      }


    $smarty->assign('QNListing',$smarty_qn_array);

    // Load our Listing Template
    $template_file = "../../skins/$defaultSkin/search_results.tpl";
    $smarty->display("file:$template_file");

    clearSmartyCompiled($SmartyCompileDir, $ClearSmartyCompiled);

    exit;

?>
